//package cn.vconinfo.base.utils;
//
//
////import com.alibaba.fastjson.JSON;
////import com.mediinfo.futurelis.common.CommonUtils;
////import com.mediinfo.futurelis.common.cache.constant.RedisTypeEnum;
////import com.mediinfo.futurelis.common.cache.constant.SerialNumberMethodEnum;
////import com.mediinfo.futurelis.common.cache.constant.SerialNumberTypeEnum;
//
////import org.apache.commons.lang.StringUtils;
//import cn.vconinfo.base.cache.ICache;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.data.redis.core.RedisCallback;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.support.atomic.RedisAtomicLong;
//import org.springframework.stereotype.Component;
//
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.ObjectInputStream;
//import java.io.ObjectOutputStream;
//import java.lang.reflect.Field;
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//import java.util.function.Function;
//import java.util.function.Supplier;
//
////import org.apache.commons.lang3.ObjectUtils;
//import org.apache.commons.lang3.StringUtils;
////import java.text.SimpleDateFormat;
//
////@ConditionalOnProperty(name = {"project.redis.enabled"}, havingValue = "true")
//@Component
//public class RedisUtil implements ICache {
//    private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
////    @Autowired
////    protected RedisTemplate<String, String> redisTemplate;
//    private static StringRedisTemplate redisTemplate;
//
//    public <T> boolean lPush(String queueName, T value) {
//        return (Boolean) RedisUtil.redisTemplate.execute((RedisCallback<Boolean>)(connection) -> {
//            connection.lPush(queueName.getBytes(), new byte[][]{this.serialize(value)});
//            return true;
//        });
//    }
//
//    public <T> T rPop(String queueName) {
//        return RedisUtil.redisTemplate.execute((RedisCallback<T>)(connection) -> {
//            byte[] value = connection.rPop(queueName.getBytes());
//            return value == null ? null : this.unSerialize(value);
//        });
//    }
//
//    public Long lLen(String queueName) {
//        return (Long) RedisUtil.redisTemplate.execute((RedisCallback<Long>)(connection) -> {
//            Long value = connection.lLen(queueName.getBytes());
//            return value == null ? null : value;
//        });
//    }
//
//    public <T> T lRang(String queueName) {
//        return RedisUtil.redisTemplate.execute((RedisCallback<T>)(connection) -> {
//            List<byte[]> value = connection.lRange(queueName.getBytes(), 0L, -1L);
//            if (value == null) {
//                return null;
//            } else {
//                List list = new ArrayList();
//                Iterator var5 = value.iterator();
//
//                while (var5.hasNext()) {
//                    byte[] byts = (byte[]) var5.next();
//                    list.add(this.unSerialize(byts));
//                }
//
//                return (T)list;
//            }
//        });
//    }
//
//    public boolean setString(String key, String value) {
//        return (Boolean) RedisUtil.redisTemplate.execute((RedisCallback<Boolean>)(connection) -> {
//            connection.set(key.getBytes(), value.getBytes());
//            return true;
//        });
//    }
//
//    public boolean setString(String key, String value, long timeout, TimeUnit timeUnit) {
//        boolean r1 = (Boolean) RedisUtil.redisTemplate.execute((RedisCallback<Boolean>)(connection) -> {
//            connection.set(key.getBytes(), value.getBytes());
//            return true;
//        });
//        boolean r2 = this.setExpire(key, timeout, timeUnit);
//        return r1 && r2;
//    }
//
//    public String getString(String key) {
//        return (String) RedisUtil.redisTemplate.execute((RedisCallback<String>)(connection) -> {
//            byte[] value = connection.get(key.getBytes());
//            return value == null ? null : new String(value);
//        });
//    }
//
//    public <T> boolean setObject(String key, T value) {
//        return (Boolean) RedisUtil.redisTemplate.execute((RedisCallback<Boolean>)(connection) -> {
//            connection.set(key.getBytes(), this.serialize(value));
//            return true;
//        });
//    }
//
//    public boolean setObject(String key, Object value, long timeout, TimeUnit timeUnit) {
//        boolean r1 = (Boolean) RedisUtil.redisTemplate.execute((RedisCallback<Boolean>)(connection) -> {
//            connection.set(key.getBytes(), this.serialize(value));
//            return true;
//        });
//        boolean r2 = this.setExpire(key, timeout, timeUnit);
//        return r1 && r2;
//    }
//
//    public <T> T getObject(String key) {
//        return RedisUtil.redisTemplate.execute((RedisCallback<T>)(connection) -> {
//            byte[] value = connection.get(key.getBytes());
//            return value == null ? null : this.unSerialize(value);
//        });
//    }
//
//    public long deleteKey(String key) {
//        return (Long) RedisUtil.redisTemplate.execute((RedisCallback<Long>)(connection) -> {
//            return connection.del(new byte[][]{key.getBytes()});
//        });
//    }
//
//    <T> byte[] serialize(T object) {
//        try {
//            ByteArrayOutputStream bao = new ByteArrayOutputStream();
//            ObjectOutputStream oos = new ObjectOutputStream(bao);
//            oos.writeObject(object);
//            return bao.toByteArray();
//        } catch (Exception var4) {
//            logger.error(var4.getMessage(), var4);
//            throw new RuntimeException(var4.getMessage(), var4);
//        }
//    }
//
//    <T> T unSerialize(byte[] bytes) {
//        try {
//            ByteArrayInputStream bao = new ByteArrayInputStream(bytes);
//            ObjectInputStream ois = new ObjectInputStream(bao);
//            return (T)ois.readObject();
//        } catch (Exception var4) {
//            logger.error(var4.getMessage(), var4);
//            throw new RuntimeException(var4.getMessage(), var4);
//        }
//    }
//
//    public boolean setExpire(String key, long timeout, TimeUnit timeUnit) {
//        return RedisUtil.redisTemplate.expire(key, timeout, timeUnit);
//    }
//
//
//
//    /**
//     * Redis加锁的操作
//     *
//     * @param key
//     * @param value
//     * @param  expireTime
//     * @return
//     */
//    public Boolean tryLock(String key, String value,long expireTime) {
//        if (redisTemplate.opsForValue().setIfAbsent(key, value, expireTime, TimeUnit.SECONDS)) {
//            return true;
//        }
//        String currentValue = redisTemplate.opsForValue().get(key);
//        if (StringUtils.isNotEmpty(currentValue) && Long.valueOf(currentValue) < System.currentTimeMillis()) {
//            //获取上一个锁的时间 如果高并发的情况可能会出现已经被修改的问题  所以多一次判断保证线程的安全
//            String oldValue = redisTemplate.opsForValue().getAndSet(key, value);
//            if (StringUtils.isNotEmpty(oldValue) && oldValue.equals(currentValue)) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//
//    /**
//     * Redis解锁的操作
//     *
//     * @param key
//     * @param value
//     */
//    public void unlock(String key, String value) {
//        String currentValue = redisTemplate.opsForValue().get(key);
//        try {
//            if (StringUtils.isNotEmpty(currentValue) && currentValue.equals(value)) {
//                redisTemplate.opsForValue().getOperations().delete(key);
//            }
//        } catch (Exception e) {
//            logger.error("unlock error:{}", e);
//        }
//    }
//
////    public boolean setString(String value, long timeout, TimeUnit timeUnit, String... keys) {
////        if (keys != null && keys.length >= 1) {
////            String key = CommonUtils.produceKey(keys);
////            return this.setString(key, value, timeout, timeUnit);
////        } else {
////            return false;
////        }
////    }
//
////    public <T> boolean setObject(T value, long timeout, TimeUnit timeUnit, String... keys) {
////        if (keys != null && keys.length >= 1) {
////            String key = CommonUtils.produceKey(keys);
////            return this.setObject(key, value, timeout, timeUnit);
////        } else {
////            return false;
////        }
////    }
//
//    public boolean setNX(String key, String value) {
//        return (Boolean) RedisUtil.redisTemplate.execute((RedisCallback<Boolean>)(connection) -> {
//            return connection.setNX(key.getBytes(), value.getBytes());
//        });
//    }
//
//    public boolean setNX(String key, String value, long timeout, TimeUnit timeUnit) {
//        boolean r1 = (Boolean) RedisUtil.redisTemplate.execute((RedisCallback<Boolean>)(connection) -> {
//            return connection.setNX(key.getBytes(), value.getBytes());
//        });
//        boolean r2 = this.setExpire(key, timeout, timeUnit);
//        return r1 && r2;
//    }
//
//    public long getIncr(String key, long timeout, TimeUnit timeUnit) {
//        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, RedisUtil.redisTemplate.getConnectionFactory());
//        Long increment = entityIdCounter.getAndIncrement();
//        if (null == increment || increment == 0L) {
//            this.setExpire(key, timeout, timeUnit);
//        }
//
//        return increment;
//    }
//
//    public long getIncr(String key) {
//        return (new RedisAtomicLong(key, RedisUtil.redisTemplate.getConnectionFactory())).getAndIncrement();
//    }
//
//    @Autowired
//    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
//        RedisUtil.redisTemplate = redisTemplate;
//    }
//
//    public long getTTL(String key, TimeUnit timeUnit) {
//        return RedisUtil.redisTemplate.getExpire(key, timeUnit);
//    }
//
//    public long getTTL(String key) {
//        return this.getTTL(key, TimeUnit.SECONDS);
//    }
//
////    public boolean hashMultiSet(RedisTypeEnum redisTypeEnum, Collection<?> collection, Long orgId) {
////        String hashKey = redisTypeEnum.getHashKey();
////        String keyType = redisTypeEnum.getKeyType();
////        if (!StringUtils.isBlank(hashKey) && !StringUtils.isBlank(keyType) && collection != null
////                && !collection.isEmpty()) {
////            if (!Objects.isNull(orgId)) {
////                hashKey = CommonUtils.produceKey(new String[]{orgId + "", hashKey});
////            }
////
////            Map<String, String> map = new HashMap(Double.valueOf((double) collection.size() / 0.75D + 1.0D).intValue());
////            collection.forEach((c) -> {
////                map.put(this.getKeyTypeValue(keyType, c), JSON.toJSONString(c));
////            });
////            RedisUtil.redisTemplate.expire(hashKey, 480L, TimeUnit.MINUTES);
////            RedisUtil.redisTemplate.opsForHash().putAll(hashKey, map);
////            return true;
////        } else {
////            return false;
////        }
////    }
//
////    public <T> List<T> hashMultiGet(RedisTypeEnum redisTypeEnum, Class<T> tClass, List<String> keys, Long orgId) {
////        String hashKey = redisTypeEnum.getHashKey();
////        if (!StringUtils.isBlank(hashKey) && !Objects.isNull(tClass) && !Objects.isNull(keys) && keys.size() >= 1) {
////            if (orgId != null) {
////                hashKey = CommonUtils.produceKey(new String[]{orgId + "", hashKey});
////            }
////
////            try {
////                List<T> list = new ArrayList();
////                List<Object> objects = RedisUtil.redisTemplate.opsForHash().multiGet(hashKey, Arrays.asList(keys.toArray()));
////                if (objects != null && !objects.isEmpty()) {
////                    Iterator var8 = objects.iterator();
////
////                    while (var8.hasNext()) {
////                        Object obj = var8.next();
////                        if (!Objects.isNull(obj)) {
////                            String text = (String) CommonUtils.covert(obj);
////                            T t1 = JSON.parseObject(text, tClass);
////                            list.add(t1);
////                        }
////                    }
////                }
////
////                return list;
////            } catch (Exception var12) {
////                logger.error(var12.getMessage(), var12);
////                return null;
////            }
////        } else {
////            return new ArrayList();
////        }
////    }
//
////    public <T> T hashGet(RedisTypeEnum redisTypeEnum, Class<T> tClass, String key, Long orgId) {
////        String hashKey = redisTypeEnum.getHashKey();
////        if (!StringUtils.isBlank(hashKey) && !Objects.isNull(tClass) && !StringUtils.isBlank(key)) {
////            List<String> keys = new ArrayList();
////            keys.add(key);
////            List<T> list = this.hashMultiGet(redisTypeEnum, tClass, keys, orgId);
////            return !Objects.isNull(list) && !list.isEmpty() ? list.get(0) : null;
////        } else {
////            return null;
////        }
////    }
//
////    public String getSerialNumber(Long orgId, SerialNumberTypeEnum serialNumberTypeEnum,
////            SerialNumberMethodEnum serialNumberMethodEnum) {
////        if (!ObjectUtils.allNotNull(new Object[]{orgId, serialNumberMethodEnum, serialNumberTypeEnum})) {
////            return null;
////        } else if (SerialNumberMethodEnum.SER_Method_0001.equals(serialNumberMethodEnum)) {
////            return this.serMethod001(orgId, serialNumberTypeEnum);
////        } else if (SerialNumberMethodEnum.SER_Method_0002.equals(serialNumberMethodEnum)) {
////            return this.serMethod002(orgId, serialNumberTypeEnum);
////        } else if (SerialNumberMethodEnum.SER_Method_0003.equals(serialNumberMethodEnum)) {
////            return this.serMethod003(orgId, serialNumberTypeEnum);
////        } else if (SerialNumberMethodEnum.SER_Method_0004.equals(serialNumberMethodEnum)) {
////            return this.serMethod004(orgId, serialNumberTypeEnum);
////        } else if (SerialNumberMethodEnum.SER_Method_0005.equals(serialNumberMethodEnum)) {
////            return this.serMethod005(orgId, serialNumberTypeEnum);
////        } else if (SerialNumberMethodEnum.WPCF_Method_0006.equals(serialNumberMethodEnum)) {
////            return this.serMethod006(orgId, serialNumberTypeEnum);
////        } else {
////            return SerialNumberMethodEnum.SER_Method_0007.equals(serialNumberMethodEnum)
////                    ? this.serMethod007(orgId, serialNumberTypeEnum)
////                    : null;
////        }
////    }
//
////    private String serMethod007(Long orgId, SerialNumberTypeEnum serialNumberTypeEnum) {
////        Date date = new Date();
////        SimpleDateFormat redisKey = new SimpleDateFormat("yyyyMMdd");
////        String key = "SerialNum:" + redisKey.format(date) + "_" + serialNumberTypeEnum + "_" + orgId;
////        long serNum = this.getIncr(key, 24L, TimeUnit.HOURS);
////        ++serNum;
////        SimpleDateFormat sf = new SimpleDateFormat("yyMMdd");
////        Long maxNum = 9999L;
////        return serNum > maxNum ? sf.format(new Date()) : sf.format(date) + StringUtils.leftPad(serNum + "", 4, "0");
////    }
//
////    private String serMethod001(Long orgId, SerialNumberTypeEnum serialNumberTypeEnum) {
////        Date date = new Date();
////        SimpleDateFormat redisKey = new SimpleDateFormat("yyyyMMdd");
////        String key = "SerialNum:" + redisKey.format(date) + "_" + serialNumberTypeEnum + "_" + orgId;
////        long serNum = this.getIncr(key, 24L, TimeUnit.HOURS);
////        ++serNum;
////        SimpleDateFormat sf = new SimpleDateFormat("yyMMdd");
////        Long maxNum = 999L;
////        return serNum > maxNum ? sf.format(new Date()) : sf.format(date) + StringUtils.leftPad(serNum + "", 3, "0");
////    }
//
////    private String serMethod002(Long orgId, SerialNumberTypeEnum serialNumberTypeEnum) {
////        Date date = new Date();
////        SimpleDateFormat redisKey = new SimpleDateFormat("yyyyMMdd");
////        String key = "SerialNum:" + redisKey.format(date) + "_" + serialNumberTypeEnum + "_" + orgId;
////        long serNum = this.getIncr(key, 24L, TimeUnit.HOURS);
////        ++serNum;
////        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
////        Long maxNum = 99999L;
////        return serNum > maxNum ? sf.format(new Date()) : sf.format(date) + StringUtils.leftPad(serNum + "", 5, "0");
////    }
//
////    private String serMethod003(Long orgId, SerialNumberTypeEnum serialNumberTypeEnum) {
////        Date date = new Date();
////        SimpleDateFormat redisKey = new SimpleDateFormat("yyyyMMdd");
////        String key = "SerialNum:" + redisKey.format(date) + "_" + serialNumberTypeEnum + "_" + orgId;
////        long serNum = this.getIncr(key, 24L, TimeUnit.HOURS);
////        ++serNum;
////        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
////        Long maxNum = 999999L;
////        return serNum > maxNum
////                ? sf.format(new Date()).substring(1)
////                : sf.format(date).substring(1) + StringUtils.leftPad(serNum + "", 6, "0");
////    }
//
////    private String serMethod004(Long orgId, SerialNumberTypeEnum serialNumberTypeEnum) {
////        String key = "SerialNum:_" + serialNumberTypeEnum + "_" + orgId;
////        long serNum = this.getIncr(key);
////        ++serNum;
////        return StringUtils.leftPad(serNum + "", 9, "0");
////    }
//
////    private String serMethod005(Long orgId, SerialNumberTypeEnum serialNumberTypeEnum) {
////        Date date = new Date();
////        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
////        String key = "SerialNum:_" + serialNumberTypeEnum + "_" + orgId;
////        long serNum = this.getIncr(key, 1L, TimeUnit.MINUTES) + 1L;
////        String newOrgId = String.valueOf(orgId);
////        if (newOrgId.length() > 12) {
////            newOrgId = newOrgId.substring(0, 12);
////        } else {
////            newOrgId = StringUtils.rightPad(newOrgId, 12, "0");
////        }
////
////        return newOrgId + sf.format(date) + StringUtils.leftPad(serNum + "", 4, "0");
////    }
//
////    private String serMethod006(Long orgId, SerialNumberTypeEnum serialNumberTypeEnum) {
////        Date date = new Date();
////        SimpleDateFormat redisKey = new SimpleDateFormat("yyyy");
////        String key = "SerialNum:" + redisKey.format(date) + "_" + serialNumberTypeEnum + "_" + orgId;
////        long serNum = this.getIncr(key, 365L, TimeUnit.DAYS);
////        ++serNum;
////        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
////        return "WPCF" + sf.format(date).substring(2) + StringUtils.leftPad(serNum + "", 8, "0");
////    }
//
//    private <T> String getKeyTypeValue(String keyType, T obj) {
//        try {
//            Field field = obj.getClass().getDeclaredField(keyType);
//            field.setAccessible(true);
//            String type = (String) field.get(obj);
//            return type;
//        } catch (Exception var5) {
//            logger.error(var5.getMessage(), var5);
//            return null;
//        }
//    }
//
//    public Long getButNoIncr(String key) {
//        key = "SerialNum:_" + key;
//        return (new RedisAtomicLong(key, RedisUtil.redisTemplate.getConnectionFactory())).get();
//    }
//
//    public String getSerialNumber(Function<Long, String> valueFunction, Supplier<String> keyFunction, int days) {
//        String key = "SerialNum:_" + (String) keyFunction.get();
//        long serNum = this.getIncr(key, (long) days, TimeUnit.DAYS);
//        ++serNum;
//        return (String) valueFunction.apply(serNum);
//    }
//}